package com.kirk.cts.ui.view.dialog;

import com.kirk.cts.core.event.CrawlerToolEvent;
import com.kirk.cts.core.event.CrawlerToolEventType;
import com.kirk.cts.core.event.CrawlerToolListener;
import com.kirk.cts.core.event.CrawlerToolObserver;
import com.kirk.cts.core.loader.LanguageLoader;
import org.apache.log4j.Logger;

import javax.swing.*;
import java.awt.*;

/**
 * <p> WaitingProgressDialog.java </p>
 *
 * @version 1.0.0
 * @Author kirkliu
 * @Date 2020-09-01 23:03
 * @Description 等待提示窗口
 */
public class WaitingProgressDialog implements Runnable, CrawlerToolListener {
    protected Logger logger = Logger.getLogger(this.getClass());
    private Thread currentThread = null;// 实际调用时就是TestThread事务处理线程

    private String messages = "";// 提示框的提示信息

    private JFrame parentFrame = null;// 提示框的父窗体

    private Dialog dialogParentFrame = null;// 提示框的父窗体

    private JDialog clueDiag = null;// “线程正在运行”提示框

    private JLabel textLabel = null;

    private WaitingProgressDialog waitingProgressDialog;

    private Dimension dimensions = Toolkit.getDefaultToolkit().getScreenSize();

    private int width = dimensions.width / 4, height = 80;

    private JProgressBar bar = new JProgressBar(JProgressBar.HORIZONTAL);

    public WaitingProgressDialog(JFrame parentFrame, Thread currentThread,
                                 String messages) {
        this.parentFrame = parentFrame;
        this.currentThread = currentThread;
        this.messages = messages;
        clueDiag = new JDialog(this.parentFrame, "正在执行，请等待...", true);
        initDiag();// 初始化提示框
    }

    public WaitingProgressDialog(Dialog dialogParentFrame, Thread currentThread,
                                 String messages) {
        this.dialogParentFrame = dialogParentFrame;
        this.currentThread = currentThread;
        this.messages = messages;
        clueDiag = new JDialog(this.dialogParentFrame, LanguageLoader.getString("ViewDetail.waitingTitle"), true);
        initDiag();// 初始化提示框

    }

    protected void initDiag() {
        waitingProgressDialog = this;
        CrawlerToolObserver.getInstance().addCrawlerListener(this);
        clueDiag.setCursor(new Cursor(Cursor.WAIT_CURSOR));
        JPanel textPanel = new JPanel();
        textLabel = new JLabel(messages);
        // 创建一条水平不确定进度条
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                // 设置在进度条中绘制完成百分比
                bar.setStringPainted(true);
                // 设置进度条的最大值和最小值,
                bar.setMinimum(0);
                clueDiag.getContentPane().add(bar, BorderLayout.CENTER);
                clueDiag.getContentPane().doLayout();
            }
        });
        clueDiag.getContentPane().add(textPanel, BorderLayout.SOUTH);
        textPanel.add(textLabel);
        (new Thread(new DisposeWaitingDialog())).start();
    }

    /*
     * (non-Javadoc)
     *
     * @see java.lang.Runnable#run()
     */

    @Override
    public void run() {
        // 显示提示框
        int left = (dimensions.width - width) / 2;

        int top = (dimensions.height - height) / 2;

        clueDiag.setSize(new Dimension(width, height));

        clueDiag.setLocation(left, top);

        clueDiag.setVisible(true);
    }

    class DisposeWaitingDialog implements Runnable {

        /*
         * (non-Javadoc)
         *
         * @see java.lang.Runnable#run()
         */
        @Override
        public void run() {
            try {
                currentThread.join();// 等待事务处理线程结束
            } catch (InterruptedException e) {
                System.out.println("Exception:" + e);
            }
            clueDiag.dispose();// 关闭提示框
            //注销监听事件
            CrawlerToolObserver.getInstance().removeCrawlerListener(waitingProgressDialog);
        }

    }

    /* (non-Javadoc)
     * @see org.javacoo.crawler.event.CrawlerListener#update(org.javacoo.crawler.event.CrawlerEvent)
     */
    @Override
    public void update(CrawlerToolEvent event) {
        if (event.getEventType().equals(CrawlerToolEventType.ProgressChangeEvent)) {
            final RunnableProgress runnableProgress = (RunnableProgress) event.getEventObject();
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    // 以总任务量作为进度条的最大值
                    bar.setMaximum(runnableProgress.getTotal());
                    // 以任务的当前完成量设置进度条的value
                    bar.setValue(runnableProgress.getCurrentNum());
                    textLabel.setText(messages + "-->" + runnableProgress.getCurrentMsg());
                }
            });
        }
    }

}
